package cn.bone.elasticsearch.dao;

import cn.bone.elasticsearch.bean.News;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * Created by MD-01 on 2017/3/21.
 */
@Repository
public class NewsDao {

    @Resource
    private JdbcTemplate template;

    public List<News> queryList(int num){
        String sql = "select news_id,news_title,news_content,news_lastupdatetime " +
                "from news where news_approvalstatus = 3 " +
                "order by news_lastupdatetime desc limit 0,?";
        List<News> news = template.query(sql, new RowMapper<News>() {
            @Override
            public News mapRow(ResultSet rs, int rowNum) throws SQLException {
                News news = new News();
                news.setNewsId(rs.getInt("news_id"));
                news.setTitle(rs.getString("news_title"));
                news.setContent(rs.getString("news_content"));
                news.setPublishTime(
                        new Date(rs.getTimestamp("news_lastupdatetime")
                                .getTime()));
                news.setType("news");
                return news;
            }
        }, num);
        return news;
    }
}
